package com.modules.controller.sys;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.controller.BaseController;
import com.common.log.LogData;
import com.common.util.MyDateUtil;
import com.common.vo.Result;
import com.modules.entity.sys.DictEntity;
import com.modules.entity.sys.DictSettingEntity;
import com.modules.service.sys.DictService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Arrays;


/**
 * 字典
 * @author yyq
 *
 */
@RestController
@RequestMapping("/sys/dict")
public class DictController extends BaseController<DictEntity>{

	@Resource
	private DictService service;
	
	/**
	 * 分页查询
	 * @param dict
	 * @return
	 */
	@GetMapping("/select")
	public Result<?> select(DictEntity dict){
		// 查询范围：改成xml查询和用户管理那里的查询一样（如果对项目/公司开放的情况下：公司可以自定义）
		LambdaQueryWrapper<DictEntity> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.like(StrUtil.isNotBlank(dict.getDictName()), DictEntity::getDictName, dict.getDictName());
		queryWrapper.like(StrUtil.isNotBlank(dict.getDictCode()), DictEntity::getDictCode, dict.getDictCode());
		queryWrapper.like(dict.getDictType() != null, DictEntity::getDictType, dict.getDictType());
		IPage<DictEntity> iPage = service.page(getPage(), queryWrapper);
		return Result.page(iPage.getRecords(), iPage.getTotal());
	}


	/**
	 * 保存
	 * @param dict
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "保存")
	@PostMapping("/save")
	public Result<?> save(@Valid DictEntity dict){
		LambdaQueryWrapper<DictEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.ne(dict.getId() != null, DictEntity::getId, dict.getId());
		wrapper.eq(DictEntity::getDictCode, dict.getDictCode());
		DictEntity one = service.getOne(wrapper);
		if(one != null) {
			return Result.fail("操作失败，重复编码");
		}
		service.saveOrUpdate(dict);
		return Result.success();
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "删除单个")
	@DeleteMapping("/delete")
	public Result<?> delete(String id){
		service.removeById(id);
		return Result.success();
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "批量删除")
	@PostMapping("/deleteBatch")
	public Result<?> deleteBatch(String ids){
		service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.success();
	}
	
	/**
	 * 查询参数设置
	 * @return
	 */
	@GetMapping("/selectSetting")
	public Result<?> selectSetting(@RequestParam(value="did",defaultValue="0",required=true) Long did,
			@RequestParam(value="page",defaultValue="1",required=true) Integer page,
			@RequestParam(value="limit",defaultValue="10",required=true) Integer limit){
		return service.selectSetting(new Page<>(page, limit), did);
	}
	
	/**
	 * 保存参数设置
	 * @param dict
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "保存")
	@PostMapping("/saveSetting")
	public Result<?> saveSetting(DictSettingEntity dict){
		return service.saveOrUpdateSetting(dict);
	}
	
	/**
	 * 删除参数设置
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "删除单个")
	@DeleteMapping("/deleteSetting")
	public Result<?> deleteSetting(String id){
		return service.removeByIdSetting(id);
	}
	
	/**
	 * 修改状态
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "修改状态")
	@PostMapping("/updateSettingState")
	public Result<?> updateSettingState(DictSettingEntity dict){
		return service.updateSettingState(dict);
	}
	
	/**
	 * 获取所有数字字典
	 * @return
	 */
	@PostMapping("/getDictAll")
	public Result<?> getDictAll(){
		return service.getDictAll();
	}

	/**
	 * 获取字典text
	 * @return
	 */
	@PostMapping("/getDictText")
	public Result<?> getDictText(String code, String value){
		return service.getDictText(code, value);
	}
	
	/**
	 * 获取字典所有参数
	 * @return
	 */
	@PostMapping("/getDictSetting")
	public Result<?> getDictSetting(String code){
		return service.getDictSetting(code);
	}
	
	

}
